import 'package:app/model/product_view_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'global.dart';

class SaveUpdatePage extends StatefulWidget {

  SaveUpdatePage({Key key,this.data,this.action}) : super(key: key);

  final ProductViewModel data;

  final String action;

  @override
  SaveUpdateState createState() => new SaveUpdateState(data,action);
}
class ProductEntity {
  int id;
  /// 产品名称
  String productName;

  /// 产品类型
  String productType;

  /// 价格
  String priceBig;

  /// 售价
  String priceSale;
  /// 图片
  String img;

  ProductEntity({
    this.id,
    this.productName,
    this.productType,
    this.priceBig,
    this.priceSale,
    this.img,
  });
}
class SaveUpdateState extends State<SaveUpdatePage> with SingleTickerProviderStateMixin  {

  SaveUpdateState(this.data,this.action);

  final ProductViewModel data;

  final String action;

  ProductEntity submitData;


  @override
  void initState() {
    submitData= new ProductEntity(id:data?.id,productName:data?.productName,priceSale: data?.priceSale,priceBig: data?.priceBig,productType: data?.productType);
  }

  @override
  Widget build(BuildContext context) {
    //submitData= new ProductEntity(id:data?.id,productName:data?.productName,priceSale: data?.priceSale,priceBig: data?.priceBig,productType: data?.productType);
    // TODO: implement build
    return new MaterialApp(
        title: 'Generated App',
        theme: new ThemeData(
        primaryColor: Global.currentTheme,
        fontFamily: "Roboto",
        ),
        home:new Scaffold(
          appBar: AppBar(
            title:Text(action),
            leading:IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: ()=>Navigator.pop(context)),
          ),
          body: new Container(
            padding: const EdgeInsets.all(16.0),
            child: new Form(
              //key: _formKey,
              child: new Column(
                children: <Widget>[
                  new TextFormField(
                    decoration: new InputDecoration(
                      labelText: '产品类型',
                    ),
                    initialValue: data?.productType,
                    onChanged: (val) { // 监听文本变化
                      submitData.productType = val;
                    },
                  ),
                  new TextFormField(
                    decoration: new InputDecoration(
                      labelText: '产品名称',
                    ),
                    initialValue: data?.productName,
                    onChanged: (val) { // 监听文本变化
                      submitData.productName = val;
                    //print(submitData.toString());
                    },
                  ),
                  new TextFormField(
                    decoration: new InputDecoration(
                      labelText: '代理价',
                    ),
                    initialValue: data?.priceBig,
                    onChanged: (val) { // 监听文本变化
                      submitData.priceBig = val;
                      //print(submitData.toString());
                    },
                  ),
                  new TextFormField(
                    decoration: new InputDecoration(
                      labelText: '售价',
                    ),
                    initialValue: data?.priceSale,
                    onChanged: (val) { // 监听文本变化
                      submitData.priceSale = val;
                    },
                  ),
                  Padding(
                    padding: const EdgeInsets.all(80.0),
                    child: Consumer<GlobalData>(
                      builder: (context,globalData,_)=>FlatButton(
                        child: Text('提交',style: TextStyle(color: Colors.white),),
                        color:globalData.currentTheme,
                        onPressed: (){
                          submitForm();
                        },
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        )
    );
  }

  void submitForm()async{
    try {
      await Dio().post("http://106.54.126.84:8099/wx/saveOrUpdateProduct",
          data:{"id":submitData?.id,"productName":submitData.productName,"priceSale":submitData.priceSale,"priceBig":submitData.priceBig,"productType":submitData.productType,
            "img":"http://img.wsola.top/app/proxy/201905141517518.png"});
      await Dio().get("http://106.54.126.84:8099/wx/reloadproductlist");
      //Provider<GlobalData>.
      Navigator.pop(context,submitData.productType);
    } catch (e) {
      print(e);
    }
  }

}
